package com.example.wearing.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wearing.R;
import com.example.wearing.javabean.Clothes;

import java.util.ArrayList;

public class ClothesOfMatchAdapter extends ArrayAdapter {
    Context context;
    ArrayList<ArrayList<String>> clothesImgOfMatch;
    int indexOfClothes[] = {0,0,0,0};
    private static final String TAG = "ClothesOfMatchAdapter";

    public ClothesOfMatchAdapter(Context context, int resource, ArrayList<ArrayList<String>> list) {
        super(context, resource, list);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = convertView;
        if (itemView == null) {
            itemView = LayoutInflater.from(getContext()).inflate(R.layout.clothes_of_match,
                    parent,
                    false);
        }

        ArrayList<String> arrayList = (ArrayList<String>) getItem(position);
        ImageView clothesImg = (ImageView) itemView.findViewById(R.id.clothesImg);

        String imgStr = arrayList.get(indexOfClothes[position]);
        Bitmap bitmap = ImageUtil.base64ToImage(imgStr);
        clothesImg.setImageBitmap(bitmap);
        return  itemView;
    }

    public void changeClothes(int position, View view){
        //点击该件衣物，可以进行更换
        ArrayList<String> arrayList = (ArrayList<String>) getItem(position);
        ImageView clothesImg = (ImageView) view.findViewById(R.id.clothesImg);

        if(indexOfClothes[position] < arrayList.size()-1)
            indexOfClothes[position]++;
        else
            indexOfClothes[position] = 0;

        String imgStr = arrayList.get(indexOfClothes[position]);
        Bitmap bitmap = ImageUtil.base64ToImage(imgStr);
        clothesImg.setImageBitmap(bitmap);
    }
}
